package com.seeyon.apps.kk.manager;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.locknlock.gw.ifgw004.IFGW004_CN_GW_SOServiceStub;
import com.locknlock.gw.ifgw004.IFGW004_CN_GW_SOServiceStub.IFGW004_CN_GW_REQ_DT;
import com.locknlock.gw.ifgw004.IFGW004_CN_GW_SOServiceStub.IFGW004_CN_GW_REQ_MT;
import com.locknlock.gw.ifgw004.IFGW004_CN_GW_SOServiceStub.IFGW004_CN_GW_RES_MT;
import com.locknlock.gw.ifgw004.IFGW004_CN_GW_SOServiceStub.ITEM_type0;
import com.locknlock.gw.ifgw004.IFGW004_CN_GW_SOServiceStub.RETURN_type0;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.kk.utils.ValueUtils;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;

public class LockBusinessTravelSapManager extends BaseManager {
	private static final Log log = LogFactory.getLog(LockBusinessTravelSapManager.class);
	
	private FreemarkFlowManager freemarkFlowManager;

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public void addBusinessForm(Long id) {
		String sql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/ccd/FIND_ALL_CCD_DATA.ftl", null);
		List<Map<String, Object>> v4 = this.jdbcTemplate.queryForList(sql, id);
		for (Map<String, Object> map : v4) {
			try {
				this.AddBussLog(map);
				Map<String, Object> resultMap = this.SendToGhr(map);
				this.UpdateLog(resultMap);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	public Map<String, Object> SendToGhr(Map<String, Object> map) throws RemoteException {
		log.info(JSONUtil.toJSONString(map));
		if (!map.isEmpty()) {
			IFGW004_CN_GW_SOServiceStub stub = new IFGW004_CN_GW_SOServiceStub();
			HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
			auth.setUsername("IFPO01");
			auth.setPassword("Passpo1$");
			stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);
			IFGW004_CN_GW_REQ_MT iFGW004_CN_GW_REQ_MT0 = new IFGW004_CN_GW_REQ_MT();
			IFGW004_CN_GW_REQ_DT param = new IFGW004_CN_GW_REQ_DT();
			ITEM_type0[] sapV2 = new ITEM_type0[map.size()];
			int num = 0;
			ITEM_type0 itp = new ITEM_type0();
			itp.setSRC_TRX_ID(MapUtils.getString(map, "邮箱唯一编号"));// 邮箱唯一ID
			itp.setEMPNO(MapUtils.getString(map, "G-HR职员编号"));// 职员编号
			itp.setSEQ(MapUtils.getString(map, "SEQ"));// 系统邮箱ID TWOID
			itp.setABS_TAKE(MapUtils.getString(map, "出差代码"));// 类型代码
			itp.setTAKE_DAY_OPT(MapUtils.getString(map, "出差类别"));// 出差类别
			itp.setPRC_BGN_DT(MapUtils.getString(map, "出差开始日期"));
			itp.setPRC_END_DT(MapUtils.getString(map, "出差结束日期"));

			itp.setPRC_BGN_TM(MapUtils.getString(map, "出差开始时间"));
			itp.setPRC_END_TM(MapUtils.getString(map, "出差结束时间"));
			itp.setTAKE_DYS(MapUtils.getString(map, "合计天数"));// 合计天数
			itp.setTAKE_HRS("");
			itp.setREASON(MapUtils.getString(map, "事由"));// 出差目的 事由
			itp.setGHR_STATUS("N");
			itp.setREMARK(MapUtils.getString(map, "事由"));
			sapV2[num] = itp;
			num++;
			param.setITEM(sapV2);
			iFGW004_CN_GW_REQ_MT0.setIFGW004_CN_GW_REQ_MT(param);

			IFGW004_CN_GW_RES_MT ifgw004_CN_GW_SO = stub.IFGW004_CN_GW_SO(iFGW004_CN_GW_REQ_MT0);
			RETURN_type0[] as = ifgw004_CN_GW_SO.getIFGW004_CN_GW_RES_MT().getRETURN();
			map.put("XIAOXI", as[0].getMESSGAE());
			map.put("STATUS", as[0].getSTATUS());
			map.put("IF_DATE", as[0].getIF_DATE());
			log.info("出差报告单,返回结果：" + JSONUtil.toJSONString(as));
		}
		return map;
	}

	public Map<String, Object> AddBussLog(Map<String, Object> vm) {
		// 日志表抓取数据
		vm.put("types", "出差报告");// 推送类型
		String selectYsj = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/lock_ccbgyuanshuju.ftl", vm);
		vm.put("source", selectYsj);// 推送源数据
		vm.put("agin", ValueUtils.getConfig().getLong("kk.CXCS"));// 是否重新传输
		vm.put("finsh", ValueUtils.getConfig().getLong("kk.WCPC"));// 完成抛送
		long mostSignificantBits = UUID.randomUUID().getMostSignificantBits();
		vm.put("SUIJI", mostSignificantBits);
		// 生成新的日志记录
		String dataXml = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/Lock_log.ftl", vm); // memberDay可以直接给
		// freemarker生成
		// 可以使用数据模板生成，这样底表事件依然有效
		FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
		try {
			FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
			for (FormExport formExport : formExportData) {
				formFactory.importBusinessFormData("locknlockadmin", "Lock_log", formExport, null);
			}
		} catch (Exception e) {
			log.error(e);
		}
		return vm;
	}

	public void UpdateLog(Map<String, Object> map) {
		Thread newThread = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String, Object> msp = new HashMap<>();
				log.info("出差报告单,更新日志 start：" + JSONUtil.toJSONString(map));
				msp.put("IFDATE", MapUtils.getString(map, "IF_DATE"));
				msp.put("STATUS", MapUtils.getString(map, "STATUS"));
				msp.put("MESSAGE", MapUtils.getString(map, "XIAOXI") == null ? " " : MapUtils.getString(map, "XIAOXI"));
				msp.put("SUI", MapUtils.getString(map, "SUIJI"));
				int updateCount = -1;
				if (MapUtils.getString(map, "STATUS") == null || MapUtils.getString(map, "STATUS").equals("")) {
					msp.put("WPS", ValueUtils.getConfig().getLong("kk.LockLogWPS"));
					String updatesql = freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/updatelogpssql.ftl", msp);
					log.info("出差报告单，更新日志 sql：" + updatesql);
					updateCount = jdbcTemplate.update(updatesql);
				} else {
					msp.put("YPS", ValueUtils.getConfig().getLong("kk.lockLogYPS"));
					String updatesql = freemarkFlowManager.fltTemplateIntoString("sql/ghr/log/updatelogsql.ftl", msp);
					log.info("出差报告单，更新日志 sql：" + updatesql);
					updateCount = jdbcTemplate.update(updatesql);
				}
				log.info("出差报告单,更新日志 end：" + JSONUtil.toJSONString(msp) + ";updateCount:" + updateCount);
			}
		});
		newThread.start();
	}
}
